package com.example.demo.controller.vip;


import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;

import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.example.demo.controller.BaseController;
import com.example.demo.domain.AjaxResult;
import com.example.demo.domain.tos.Tos;
import com.example.demo.domain.tovip.Tovip;
import com.example.demo.domain.vip.Vip;
import com.example.demo.service.vip.VipService;
import com.example.demo.web.TableDataInfo;



/***
 * @author zby
 */
@Controller
@RequestMapping("/vip")
public class VipController extends BaseController {
	@Autowired
	private VipService vipService;

	@RequestMapping("/")
	public String index() {

		return "index";
	}
    //加载服务管理的页面
	@GetMapping("")
	public String vip() {
		return "vip/vip";
	}

	//查询方法，并将结果渲染到表格中
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(Vip vip) {
		startPage();
		SimpleDateFormat comPTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		
		List<Vip> list = vipService.selectVipList(vip);
		for(Vip e:list) {
	
			if(comPTime.format(e.getVipExp()).compareTo(comPTime.format(new Date()))<0) {
				e.setVipState(2);
				vipService.updateByPrimaryKey(e);
//				System.out.println(e.getVipExp()+"与"+new Date()+"="+comPTime.format(e.getVipExp()).compareTo(comPTime.format(new Date())));
			}
			System.out.println(comPTime.format(e.getVipExp())+"与"+comPTime.format(new Date())+"="+comPTime.format(e.getVipExp()).compareTo(comPTime.format(new Date())));

			
			
		}
		return getDataTable(list);
	}
	
	@GetMapping("/add")
	public String add() {
		return  "vip"+"/add";
	}
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult add(Vip vip) {
		return toAjax(vipService.insert(vip));
	}
	
	/**
	 * 修改
	 * 
	 * 
	 */
	//从前端获取选中的信息
	@GetMapping("/edit/{vipId}")
	public String edit(@PathVariable("vipId") Integer vipId, ModelMap mmap) {
	Vip	vip = vipService.selectByPrimaryKey(vipId);
		mmap.put("vip", vip);
		return  "vip/edit";
	}
	
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(Vip vip) {
		
		return toAjax(vipService.updateByPrimaryKey(vip));
	}
	
	/**
	 * 充值
	 */
	//从前端获取选中的信息
	@GetMapping("/recharge/{vipId}")
	public String recharge(@PathVariable("vipId") Integer vipId, ModelMap mmap) {
	Vip	vip = vipService.selectByPrimaryKey(vipId);
		mmap.put("vip", vip);
		return  "vip/recharge";
	}
	
	@PostMapping("/recharge")
	@ResponseBody
	public AjaxResult rechargeSave(Vip vip) {
		Vip n = vipService.selectByPrimaryKey(vip.getVipId());
		System.out.println(vip.getVipAccount()+"   "+n.getVipAccount());
		BigDecimal TotalAccount = vip.getVipAccount().add(n.getVipAccount());
//		System.out.println(vip.get);
		vip.setVipAccount(TotalAccount);
		return toAjax(vipService.updateByPrimaryKey(vip));
	}

	/**
	 * 删除广告
	 */
	
	@PostMapping("/remove")
	@ResponseBody
	public AjaxResult remove(String ids) {			
		String[] id = ids.split(",");
		return toAjax(vipService.deleteVipByIds(id));
	}
	
	//查询服务类型名称
	@PostMapping("/selectTovipName")
	@ResponseBody
	public List<Tovip> selectTovipName() {
		
		List<Tovip> tovip = vipService.selectTovipName();
		
		return tovip;
	}

}
